#include "serverHead/head.h"
#include "serverHead/taskQueue.h"

int exitPipe[2];
// 注册SIGPIPE信号
void handler(int signum){
    printf("等待退出\n");
    write(exitPipe[1], "1", 1);
}

int main(int argc, char* argv[])
{   
    // ./server ../conf/server.conf
    ARGS_CHECK(argc, 2);

    int fd1 = open(argv[1], O_RDONLY);
    ERROR_CHECK(fd1, -1, "open file");

    char buf[1024] = {0};
    read(fd1, buf, sizeof(buf));
    const char *pIp;
    const char *pPort;
    const char *pNum;
    pIp = strtok(buf, " ");
    pPort = strtok(NULL, " ");
    pNum = strtok(NULL, " ");

    pipe(exitPipe);
   
//    if(fork() != 0){
//        printf("pid = %d\n", getpid());
//        close(exitPipe[0]);
//        signal(SIGPIPE, handler);
//        wait(NULL);
//        exit(0);
//    }
    close(exitPipe[1]);
   
    // 准备线程池的数据结构
    threadPool_t threadPool;
    int workernum = atoi(pNum);
    threadPoolInit(&threadPool, workernum);
    // 创建子线程
    makeWorker(&threadPool);

    //tcp连接
    int sockfd;
    tcpInit(pIp, pPort, &sockfd);

    //监听
    int epfd = epoll_create(1);
    epollAdd(epfd, sockfd);
    epollAdd(epfd, exitPipe[0]);
    struct epoll_event readyset[1024];

    userInfo user[1024];
    int userNum = 0;
    train_t* train = (train_t*)calloc(1, sizeof(train_t));
    while(1){
        int readynum = epoll_wait(epfd, readyset, 1024,-1);
        for(int i = 0; i < readynum; ++i){
            if(readyset[i].data.fd == sockfd){ 
                int netfd = accept(sockfd,NULL,NULL);
                bzero(&user[userNum], sizeof(userInfo));
                recvTrain(netfd, train);
                user[userNum].netfd = netfd;
                user[userNum].islogin = LOGIN;
                // 初始化到根目录
                strcpy(user[userNum].root, "ROOT");
                // 客户端需要传用户名过来
                strcpy(user[userNum].my_pathname, train->data);
                       
                epollAdd(epfd, user[userNum].netfd);
                userNum++;
            }
            else if(readyset[i].data.fd == exitPipe[0]);
            // 接收到客户端来的消息
            else{
                for(int j = 0; j<= userNum; ++j){
                    if((readyset[i].data.fd == user[j].netfd) && (user[j].islogin == LOGIN)) {
                        pthread_mutex_lock(&threadPool.taskQueue.mutex);
                        bzero(train, sizeof(train_t));
                        ssize_t ret = recvTrain(user[j].netfd, train);
                        printf("接收到消息%s\n", train->data);
                        if(ret == 0){
                            printf("连接关闭！\n");
                            user[j].islogin = LOGOUT;
                            epollDel(epfd, user[j].netfd);
                            close(user[j].netfd);
                            break;
                        }
                        taskEnQueue(&threadPool.taskQueue, train, &user[j]);
                        printf("主线程发送一个任务\n");
                        pthread_cond_signal(&threadPool.taskQueue.cond);
                        pthread_mutex_unlock(&threadPool.taskQueue.mutex);
                    }
                }
            }
        }
    }
    return 0;
}

